package leetcode_301_400;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class LeeCode_383 {
    public static void main(String[] args) {
        System.out.println(canConstruct("aa", "ab"));
    }
    private static boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> map1 = new HashMap<>();
        Map<Character, Integer> map2 = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            char c = magazine.charAt(i);
            if (map2.containsKey(c)){
                map2.put(c, map2.get(c) + 1);
                continue;
            }
            map2.put(c, 1);
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            char c = ransomNote.charAt(i);
            if (map1.containsKey(c)){
                map1.put(c, map1.get(c) + 1);
                if (!map2.containsKey(c) || map1.get(c) > map2.get(c)){
                    return false;
                }
                continue;
            }
            map1.put(c, 1);
            if (!map2.containsKey(c))
                return false;
        }
        return true;
    }
}
